package com.rjwh.dingdong.client.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.rjwh.dingdong.client.R;

/**
 * @author woniu
 * @ClassName 进度数值显示在上面的进度条
 * @Description
 * @date 16/7/7
 * @history 16/7/7 author: description:
 */
public class FloatTextProgressBar extends View {

    protected Context context;
    /**画笔*/
    protected Paint paint;

    /**整个控件宽度*/
    protected float width;

    /**整个控件高度*/
    protected float height;

    /**进度条未填充颜色*/
    protected int backgroundColor;

    /**进度条填充颜色*/
    protected int fillColor;

    /**文字颜色*/
    protected int textColor;

    /**进度条*/
    protected float progress;

    /**当前进度具体数值*/
    private int progressCount;

    /**进度条高度*/
    private float progressHeight;

    /**进度条宽度*/
    protected float progressWidth;

    /**浮动框宽度*/
    private float floatRectWidth;

    /**浮动框高度*/
    private float floatRectHeight;

    /**浮动框左右边距*/
    private float margin;

    /**文字大小*/
    private float textSize;


    public FloatTextProgressBar(Context context) {
        super(context);
        this.context = context;
        initPaint();
    }

    public FloatTextProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context, attrs);
        initPaint();
    }

    public FloatTextProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(context, attrs);
        initPaint();
    }

    /**
     * 初始化
     *
     */
    private void init(Context context, AttributeSet attrs) {
        final TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.floatTextProgressBar);
        progress = a.getInteger(R.styleable.floatTextProgressBar_progressNow, 0);
        progressCount = a.getInteger(R.styleable.floatTextProgressBar_progress_count, 0);
        fillColor = a.getColor(R.styleable.floatTextProgressBar_fillColor, 0xffff0000);
        backgroundColor = a.getColor(R.styleable.floatTextProgressBar_backgroundColor, 0xfff4f4f4);
        textColor = a.getColor(R.styleable.floatTextProgressBar_textColor, 0xffffffff);
        textSize = a.getDimension(R.styleable.floatTextProgressBar_textSize, sp2px(10));
        setProgress(progress);
        a.recycle();
    }


    /**
     * 初始化画笔
     */
    protected void initPaint() {
        if (paint == null) {
            paint = new Paint();
        } else {
            paint.reset();
        }
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        getDimension();
        drawProgress(canvas);
        drawText(canvas);
    }

    protected void getDimension() {
        width = getWidth();
        height = getHeight();
        progressHeight = height / 5;
        progressWidth = (float) (progress / 100.0 * width);
        floatRectWidth = height / 5 * 4;
        floatRectHeight = height / 9 * 4;
        margin = dip2px(3);
    }

    private void drawProgress(Canvas canvas) {
        //绘制未填充进度条
        paint.setColor(backgroundColor);
        RectF backgroundRectF = new RectF(0, height - progressHeight, width, height);
        canvas.drawRoundRect(backgroundRectF, progressHeight / 2, progressHeight / 2, paint);

        //绘制填充条
        paint.setColor(fillColor);
        RectF fillRectF = new RectF(0, height - progressHeight, progressWidth, height);
        canvas.drawRoundRect(fillRectF, progressHeight / 2, progressHeight / 2, paint);
    }

    private void drawText(Canvas canvas) {
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        float textWidth = paint.measureText(String.valueOf(progressCount));
        if (progress > 0) {
            if (progressWidth < floatRectWidth + margin) {
                canvas.drawText(String.valueOf(progressCount), margin + floatRectWidth / 2 - textWidth / 2, floatRectHeight / 2 + textSize / 4 + margin, paint);
            } else if (width - progressWidth < floatRectWidth + margin) {
                canvas.drawText(String.valueOf(progressCount), width - margin - floatRectWidth / 2 - textWidth / 2, floatRectHeight / 2 + textSize / 4 + margin, paint);
            } else {
                canvas.drawText(String.valueOf(progressCount), progressWidth - textWidth / 2, floatRectHeight / 2 + textSize / 4 + margin, paint);
            }
        }
    }

    /**
     * 设置进度
     *
     */
    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    /**
     * 设置无填充色
     *
     */
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    /**
     * 设置文字颜色
     *
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    /**
     * 设置填充色
     *
     */
    public void setFillColor(int fillColor) {
        this.fillColor = fillColor;
    }

    public void setProgressCount(int progressCount) {
        this.progressCount = progressCount;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    /**
     * dp转px
     *
     */
    private float dip2px(float dpValue) {
        final float scale = this.context.getResources().getDisplayMetrics().density;
        return dpValue * scale + 0.5f;
    }

    /**
     * sp转px
     *
     */
    private float sp2px(float sp) {
        final float scale = getResources().getDisplayMetrics().scaledDensity;
        return sp * scale;
    }
}
